package com.jc.ecg;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private CarlECG mCrlEcg;
    /**
     * 停止
     */
    private Button mBtnStop;
    /**
     * 重置
     */
    private Button mBtnReset;
    /**
     * Y缩放：1
     */
    private Button mBtnScale1;
    /**
     * Y缩放：0.5
     */
    private Button mBtnScale5;
    /**
     * Y缩放：0.2
     */
    private Button mBtnScale2;
    /**
     * 添加数据（立即画）
     */
    private Button mBtnAddDateNow;
    /**
     * 添加数据
     */
    private Button mBtnAddDateLazy;
    /**
     * X间距：5
     */
    private Button mBtnSpeed5;
    /**
     * X间距：10
     */
    private Button mBtnSpeed10;
    /**
     * X间距：20
     */
    private Button mBtnSpeed20;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mCrlEcg = (CarlECG) findViewById(R.id.crlEcg);
        mBtnStop = (Button) findViewById(R.id.btnStop);
        mBtnStop.setOnClickListener(this);
        mCrlEcg.setOnClickListener(this);
        mBtnReset = (Button) findViewById(R.id.btnReset);
        mBtnReset.setOnClickListener(this);
        mBtnScale1 = (Button) findViewById(R.id.btnScale1);
        mBtnScale1.setOnClickListener(this);
        mBtnScale5 = (Button) findViewById(R.id.btnScale5);
        mBtnScale5.setOnClickListener(this);
        mBtnScale2 = (Button) findViewById(R.id.btnScale2);
        mBtnScale2.setOnClickListener(this);
        mBtnAddDateNow = (Button) findViewById(R.id.btnAddDateNow);
        mBtnAddDateNow.setOnClickListener(this);
        mBtnAddDateLazy = (Button) findViewById(R.id.btnAddDateLazy);
        mBtnAddDateLazy.setOnClickListener(this);
        mBtnSpeed5 = (Button) findViewById(R.id.btnSpeed5);
        mBtnSpeed5.setOnClickListener(this);
        mBtnSpeed10 = (Button) findViewById(R.id.btnSpeed10);
        mBtnSpeed10.setOnClickListener(this);
        mBtnSpeed20 = (Button) findViewById(R.id.btnSpeed20);
        mBtnSpeed20.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStop:
                mCrlEcg.toggle();
                break;
            case R.id.crlEcg:
                break;
            case R.id.btnReset:
                mCrlEcg.reset();
                break;
            case R.id.btnScale1:
                mCrlEcg.setScaleY(1);
                break;
            case R.id.btnScale5:
                mCrlEcg.setScaleY(0.5f);
                break;
            case R.id.btnScale2:
                mCrlEcg.setScaleY(0.2f);
                break;
            case R.id.btnAddDateNow:
                mCrlEcg.addData(getDatas(1), true);
                break;
            case R.id.btnAddDateLazy:
                mCrlEcg.addData(getDatas(-1), false);
                break;
            case R.id.btnSpeed5:
                mCrlEcg.setStepWidth(5);
                break;
            case R.id.btnSpeed10:
                mCrlEcg.setStepWidth(10);
                break;
            case R.id.btnSpeed20:
                mCrlEcg.setStepWidth(20);
                break;
        }
    }

    private List<Integer> getDatas(int flag) {
        List<Integer> temps = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            temps.add((int) (Math.random() * 100 * flag));
        }
        return temps;
    }
}
